<script type="text/javascript" charset="utf-8" src="__PUBLIC__/admin/js/uploadify/jquery.uploadify.min.js" ></script>
<link rel="stylesheet" charset="utf-8" type="text/css" href="__PUBLIC__/admin/js/uploadify/uploadify.css"> 
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/admin/js/upload.js" ></script>
<!-- 配置文件 -->
<script type="text/javascript" src="__PUBLIC__/admin/js/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="__PUBLIC__/admin/js/ueditor/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
	var ue = UE.getEditor('edit');
</script>

<style>
.bg{ background: none; border: none;}
.uploadify-button-text{ display: inline-flex; }
.uploadify-button-text button{ margin: -10px; }
.upviewer img{ height:90px; width: 120px; }
.upviewer ul{ margin:0px; padding:0px; }
.upviewer li{ margin:0px; padding:0px; position: relative; display:inline-block; width:120px; height:90px; background-position: center; background-size: contain; background-repeat: no-repeat; float: left; margin-right:10px; border:1px #ccc solid; background-color:#f0f0f0;}
.upviewer li a{ display:inline-block; position: absolute; bottom:0px; left:0px; text-align:center; width:100%; background-color:rgba(0,0,0,0.3); color:red; cursor: pointer; }
.upviewer li:hover a{ background-color:rgba(0,0,0,0.8); }
</style>
<div class="container" style="padding-top: 50px;">
	<div class="row clearfix">
		<div class="col-md-12 column">
			<form class="form-horizontal" role="form">
				
				<div class="form-group">
					 <label for="title" class="col-sm-2 control-label">标题:</label>
					<div class="col-sm-6">
						<input type="text" class="form-control" id="title" />
					</div>
				</div>
				
				<div class="form-group">
					 <label for="category" class="col-sm-2 control-label">选择类别:</label>
					<div class="col-sm-4">
							<select class="form-control" id="category">
								<option value="0" >请选择</option>
								<volist name="category" id="list" >
									<if condition="$list.pid neq 0">
										<option value="{$list.id}">{$list.category}</option>
									</if>
								</volist>
							</select>
					</div>
				</div>
				
				<div class="form-group">
					 <label for="category" class="col-sm-2 control-label">关联广告:</label>
					<div class="col-sm-4">
							<select class="form-control" id="advertising">
								<option value="0">不关联</option>
									<volist name="advertising" id="data" >
									<option value="{$data.id}">{$data.title}</option>
									</volist>
							</select>
					</div>
				</div>
				
				<div class="form-group">
					<label for="title" class="col-sm-2 control-label">分享内容:</label>
					<div class="col-sm-6">
						<textarea class="form-control" name="share_content" id="share_content" rows="3"></textarea>
					</div>
				</div>
				
				<div class="form-group">
					 <label for="inputPassword3" class="col-sm-2 control-label">展示图片:</label>
					<div class="col-sm-10">
						<input type="file" class="form-control" id="upload_list" ><br />    	  
				    	<div id="view_list" class="upviewer" ></div>
					</div>
				</div>

				<div class="form-group">
                    <label for="share_img" class="col-sm-2 control-label">分享图片300x300:<span style="color: red;">*</span></label>
                    <div class="col-sm-10 js_upload_img">
                        <input type="file" class="form-control" id="share_img" value=""><br /> 
                        <div name="img" class="upviewer">
                            <ul>
                            </ul>
                        </div>
                    </div>
                </div>
				
				<div class="form-group">
					<label class="col-sm-2 control-label">娱头条:</label>
					<div class="col-sm-10">
						<label for="is_top1" class="control-label">
							<input type="radio" id="is_top1" name="is_top" value="0" checked>否
						</label>
						<label for="is_top2" class="control-label">
							<input type="radio" id="is_top2" name="is_top" value="1">是
						</label>
					</div>
				</div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">系统:</label>
                    <div class="col-sm-10">
                        <label for="system1" class="control-label">
                            <input type="checkbox" name="systems" value="android">Android
                        </label>
                        <label for="system2" class="control-label">
                            <input type="checkbox" name="systems" value="ios">Ios
                        </label>
                    </div>
                </div>
				<div class="form-group">
					 <label for="inputPassword3" class="col-sm-2 control-label">内容:</label>
					<div class="col-sm-10" >
					<textarea id="edit" name="content" type="text/plain"></textarea>
					</div>
				</div>
				
				<div class="form-group">
					<div class="col-sm-offset-2 col-sm-10">
						 <button type="button" class="btn btn-primary button">提交</button>
					</div>
				</div>
			</form>
		</div>
</div>
<script>
$(function(){
			
initDelFile();
	//多文件上传
initUploadify({ ID:'upload_list',url:"{:U('Upload/upload')}",success:function(obj){ 
		if(obj.s){
			$('#view_list').append('<li><img src="'+obj.d+'"/><a d_path="'+obj.d+'">删除</a></li>');
			initDelFile();
		}else{
			alert(obj.m);
		}
	} 
});

	 
function initDelFile(){
	$('.upviewer li a').unbind('click').click(function(){
		if(!confirm('你确认删除吗?')) return false;
		var _this = $(this);
		var dpath = $(_this).attr('d_path');
		if(!dpath) return false;
		$.post("{:U('Upload/delfile')}",{filepath:dpath},function(obj){ 
			if(obj.s){ 
				$(_this).parents('li').remove();
			}else{
				alert(obj.m);
				$(_this).parents('li').remove();
			} 
		},'json');
	});
}
	 
	 $(".button").click(function(){
	 	var title = $('#title').val();
	 	
	 	var list_src=[];
	 	$("#view_list").find("img").each(function(){
	 		list_src.push($(this).attr('src'));
	 	});
	 	var category=$('#category').val();
	 	var aid=$('#advertising').val();
	 	var content = ue.getContent();
	 	
	 	if(title==""){
	 		alert('请输入标题');
	 		return false;
	 	}
	
	 	if(category==0){
	 		alert('请选择类别');
	 		return false;
	 	}
	
	 	
	 	// if($("#view_list").find("img").length!=1){
	 	// 	alert('请上传一张展示图片');
	 	// 	return false;
	 	// }
	 	
	 	if(content==""){
	 		alert('请输入内容');
	 		return false;
	 	}
		var systems = '';
		$("[name='systems']:checked").each(function(index, el) {
			systems +=(systems ? ',':'')+$(el).val();
		});
	 	var url = "{:U('Article/add_article')}"
	 	var parm = {
	 		
	 		title:title,
	 		category:category,
	 		list_src:list_src,
	 		content:content,
	 		aid:aid,
			share_content:$("[name='share_content']").val(),
			share_img:$("[name='share_img']").val(),
			is_top:$("input[name='is_top']:checked").val(),
			systems:systems
	 	}
	 	
	 	$.post(url,parm,function(obj){
	 		if(obj.s){
	 			alert(obj.m);
	 			location.href="{:U('Article/index')}";
	 		}
	 	})
	 	
	 	
	 })
	 
	 
	});
</script>
